using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Forms;
using ZoneFiveSoftware.Common.Visuals;
using FilteredStatistics.Common;
using FilteredStatistics.Common.Controller;
using FilteredStatistics.Controller;
using FilteredStatistics.Data;

namespace FilteredStatistics.View
{
    partial class FilteredStatisticsSettingsControl : UserControl
    {
        public FilteredStatisticsSettingsControl()
        {
            InitializeComponent();

            PluginMain.LogbookChanged += new PluginMain.LogbookChangedEventHandler(OnLogbookChanged);
            Options.Instance.OptionsChanged += new Options.OptionsChangedEventHandler(OnOptionsChanged);
            TemplateManager.Instance.PropertyChanged += new PropertyChangedEventHandler(OnTemplateManagerPropertyChanged);

            ChartLineColorSelector.Selected = Options.Instance.ChartLineColor;
            ChartLineColorSelector.ButtonImage = Resources.Resources.DropDownIcon;
            ChartFillColorSelector.Selected = Options.Instance.ChartFillColor;
            ChartFillColorSelector.ButtonImage = Resources.Resources.DropDownIcon;
            ChartSelectedColorSelector.Selected = Options.Instance.ChartSelectedColor;
            ChartSelectedColorSelector.ButtonImage = Resources.Resources.DropDownIcon;
            ChartValidRegionColorSelector.Selected = Options.Instance.ChartValidRegionColor;
            ChartValidRegionColorSelector.ButtonImage = Resources.Resources.DropDownIcon;
            ColorColumnsCheckBox.Checked = Options.Instance.DrawColumnsColored;
            UseMapOverlayHighlightCheckBox.Checked = Options.Instance.UseMapOverlayHighlight;
            UseMapOverlayHighlightCheckBox.Enabled = MapOverlayPlugin.Instance.IsPluginInstalled &&
                                                     MapOverlayPlugin.Instance.IsHighlightFunctionAvailable &&
                                                     MapOverlayPlugin.Instance.IsHighlightEnabledGetterAvailable;
            ShowTemplatesModifiedWarningsCheckBox.Checked = Options.Instance.ShowTemplatesModifiedWarning;
            SplitsCriteriaWarningCheckBox.Checked = Options.Instance.ShowSplitsTemplatesWarning;

            TemplateFilterListControl.FilterTypeColumn.Width = Options.Instance.OptionsCriteriaTypesColumnWidth;
            TemplateFilterListControl.ItemColumn.Width = Options.Instance.OptionsNamedZonesColumnWidth;

            DonateImageLabel.Image = Resources.Resources.DonateImage;

            InitializeTemplatesControls();
        }

        void OnLogbookChanged(object sender, ZoneFiveSoftware.Common.Data.Fitness.ILogbook oldLogbook, ZoneFiveSoftware.Common.Data.Fitness.ILogbook newLogbook)
        {
            Refresh();
        }

        void OnOptionsChanged(System.ComponentModel.PropertyChangedEventArgs changedProperty)
        {
            ChartLineColorSelector.Selected = Options.Instance.ChartLineColor;
            ChartFillColorSelector.Selected = Options.Instance.ChartFillColor;
            ChartSelectedColorSelector.Selected = Options.Instance.ChartSelectedColor;
            ColorColumnsCheckBox.Checked = Options.Instance.DrawColumnsColored;
            ShowTemplatesModifiedWarningsCheckBox.Checked = Options.Instance.ShowTemplatesModifiedWarning;
            SplitsCriteriaWarningCheckBox.Checked = Options.Instance.ShowSplitsTemplatesWarning;

            TemplateFilterListControl.FilterTypeColumn.Width = Options.Instance.OptionsCriteriaTypesColumnWidth;
            TemplateFilterListControl.ItemColumn.Width = Options.Instance.OptionsNamedZonesColumnWidth;
        }

        void OnTemplateManagerPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Templates")
            {
                InitializeTemplatesControls();
            }
            else if (e.PropertyName == "Template")
            {
                Template modifiedTemplate = sender as Template;

                Debug.Assert(modifiedTemplate != null);

                if (modifiedTemplate.Name == TemplatesComboBox.SelectedItem as String)
                {
                    TemplateFilterListControl.Filters = modifiedTemplate.Filters;
                    HideInDailyActivityCheckBox.Checked = modifiedTemplate.ShowOnlyInSettings;
                }
            }
        }

        public override void Refresh()
        {
            UseMapOverlayHighlightCheckBox.Checked = Options.Instance.UseMapOverlayHighlight;
            UseMapOverlayHighlightCheckBox.Enabled = MapOverlayPlugin.Instance.IsPluginInstalled &&
                                                     MapOverlayPlugin.Instance.IsHighlightFunctionAvailable &&
                                                     MapOverlayPlugin.Instance.IsHighlightEnabledGetterAvailable &&
                                                     MapOverlayPlugin.Instance.IsHighlightEnabled;

            InitializeTemplatesControls();

            base.Refresh();
        }

        private void ChartLineColorSelector_SelectedChanged(object sender, System.EventArgs e)
        {
            Options.Instance.ChartLineColor = ChartLineColorSelector.Selected;
        }

        private void ChartFillColorSelector_SelectedChanged(object sender, System.EventArgs e)
        {
            Options.Instance.ChartFillColor = ChartFillColorSelector.Selected;
        }

        private void ChartSelectedColorSelector_SelectedChanged(object sender, System.EventArgs e)
        {
            Options.Instance.ChartSelectedColor = ChartSelectedColorSelector.Selected;
        }

        private void ChartValidRegionColorSelector_SelectedChanged(object sender, EventArgs e)
        {
            Options.Instance.ChartValidRegionColor = ChartValidRegionColorSelector.Selected;
        }

        private void ColorColumnsCheckBox_CheckedChanged(object sender, System.EventArgs e)
        {
            Options.Instance.DrawColumnsColored = ColorColumnsCheckBox.Checked;
        }

        private void UseMapOverlayHighlightCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            Options.Instance.UseMapOverlayHighlight = UseMapOverlayHighlightCheckBox.Checked;
        }

        private void ShowTemplatesModifiedWarningsCheckBox_CheckedChanged(object sender, System.EventArgs e)
        {
            Options.Instance.ShowTemplatesModifiedWarning = ShowTemplatesModifiedWarningsCheckBox.Checked;
        }

        private void SplitsCriteriaWarningCheckBox_CheckedChanged(object sender, System.EventArgs e)
        {
            Options.Instance.ShowSplitsTemplatesWarning = SplitsCriteriaWarningCheckBox.Checked;
        }

        private void TemplatesComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (TemplatesComboBox.Items.Count > 0 && TemplatesComboBox.SelectedIndex != -1)
            {
                RenameTemplateButton.Enabled = true;
                DeleteTemplateButton.Enabled = true;
                HideInDailyActivityCheckBox.Enabled = true;

                Template selectedTemplate = TemplateManager.Instance.GetTemplate(TemplatesComboBox.SelectedItem as String);

                TemplateFilterListControl.RestrictedTemplate = selectedTemplate;
                TemplateFilterListControl.Filters = selectedTemplate.Filters;
                HideInDailyActivityCheckBox.Checked = selectedTemplate.ShowOnlyInSettings;
            }
            else
            {
                RenameTemplateButton.Enabled = false;
                DeleteTemplateButton.Enabled = false;
                HideInDailyActivityCheckBox.Enabled = false;
            }
        }

        private void RenameTemplateButton_Click(object sender, EventArgs e)
        {
            Template selectedTemplate = TemplateManager.Instance.GetTemplate(TemplatesComboBox.SelectedItem as String);
            AddRenameTemplateDialog dlg = new AddRenameTemplateDialog(selectedTemplate);
            DialogResult result;

            result = dlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                TemplateManager.Instance.RenameTemplate(selectedTemplate, dlg.TemplateName);

                // Update UI & reselect the same template
                InitializeTemplatesControls();
                TemplatesComboBox.SelectedItem = dlg.TemplateName;
            }
        }

        private void AddTemplateButton_Click(object sender, EventArgs e)
        {
            AddRenameTemplateDialog dlg = new AddRenameTemplateDialog(null);
            DialogResult result;

            result = dlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                Template newTemplate = new Template(dlg.TemplateName, new FilterList(null));
                TemplateManager.Instance.AddTemplate(newTemplate);

                // Update UI & reselect the same template
                TemplatesComboBox.SelectedItem = dlg.TemplateName;
            }
        }

        private void DeleteTemplateButton_Click(object sender, EventArgs e)
        {
            DialogResult result;
            String message = String.Format(PluginView.GetLocalizedString("DeleteTemplateWarningText"), TemplatesComboBox.SelectedItem as String);

            result = MessageBox.Show(message,
                                     PluginView.GetLocalizedString("WarningText"),
                                     MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (result == DialogResult.OK)
            {
                TemplateManager.Instance.RemoveTemplate(TemplatesComboBox.SelectedItem as String);
            }
        }

        private void TemplateFilterListControl_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
        {
            if (e.Column == TemplateFilterListControl.FilterTypeColumn)
            {
                Options.Instance.OptionsCriteriaTypesColumnWidth = TemplateFilterListControl.FilterTypeColumn.Width;
            }
            else if (e.Column == TemplateFilterListControl.ItemColumn)
            {
                Options.Instance.OptionsNamedZonesColumnWidth = TemplateFilterListControl.ItemColumn.Width;
            }
        }
        
        private void HideInDailyActivityCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            Template selectedTemplate = TemplateManager.Instance.GetTemplate(TemplatesComboBox.SelectedItem as String);

            if (selectedTemplate != null)
            {
                selectedTemplate.ShowOnlyInSettings = HideInDailyActivityCheckBox.Checked;
            }
        }

        private void DonateImageLabel_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=4B244CKJR5STE");
        }

        public void ThemeChanged(ITheme visualTheme)
        {
            ChartLineColorSelector.ThemeChanged(visualTheme);
            ChartFillColorSelector.ThemeChanged(visualTheme);
            ChartSelectedColorSelector.ThemeChanged(visualTheme);
            ChartValidRegionColorSelector.ThemeChanged(visualTheme);

            TemplateFilterListControl.ThemeChanged(visualTheme);
        }

        public void UICultureChanged(CultureInfo culture)
        {
            TemplateFilterListControl.UICultureChanged(culture);

            DisplayOptionsGroupBox.Text = PluginView.GetLocalizedString("DisplayOptionsGroupBoxText");
            WarningsGroupBox.Text = PluginView.GetLocalizedString("WarningsText");
            TemplatesGroupBox.Text = PluginView.GetLocalizedString("TemplatesText");

            ChartLineColorLabel.Text = PluginView.GetLocalizedString("ChartLineColorLabelText");
            ChartFillColorLabel.Text = PluginView.GetLocalizedString("ChartFillColorLabelText");
            ChartSelectedColorLabel.Text = PluginView.GetLocalizedString("ChartSelectedColorLabelText");
            ChartValidRegionColorLabel.Text = PluginView.GetLocalizedString("ChartValidRegionColorLabelText");
            ColorColumnsCheckBox.Text = PluginView.GetLocalizedString("ColorColumnsCheckBoxText");
            UseMapOverlayHighlightCheckBox.Text = PluginView.GetLocalizedString("UseMapOverlayHighlightCheckBoxText");

            ShowTemplatesModifiedWarningsCheckBox.Text = PluginView.GetLocalizedString("ShowTemplatesModifiedWarningsCheckBoxText");
            SplitsCriteriaWarningCheckBox.Text = PluginView.GetLocalizedString("ShowSplitsCriteriaWarningsCheckBoxText");

            AddTemplateButton.Text = PluginView.GetLocalizedString("NewTemplateButtonText");
            DeleteTemplateButton.Text = PluginView.GetLocalizedString("DeleteText");
            RenameTemplateButton.Text = PluginView.GetLocalizedString("RenameTemplateButtonText");
            HideInDailyActivityCheckBox.Text = PluginView.GetLocalizedString("HideInDailyActivityCheckBoxText");
        }

        private void InitializeTemplatesControls()
        {
            int templateCount = TemplateManager.Instance.GetTemplateCount();

            TemplatesComboBox.Items.Clear();

            for (int i = 0; i < templateCount; ++i)
            {
                Template currentTemplate = TemplateManager.Instance.GetTemplate(i);

                TemplatesComboBox.Items.Add(currentTemplate.Name);
            }

            TemplateFilterListControl.Enabled = TemplatesComboBox.Items.Count > 0;
            TemplatesComboBox.Enabled = TemplateFilterListControl.Enabled;
            RenameTemplateButton.Enabled = TemplateFilterListControl.Enabled;
            DeleteTemplateButton.Enabled = TemplateFilterListControl.Enabled;
            HideInDailyActivityCheckBox.Checked = TemplateFilterListControl.Enabled;

            if (TemplateFilterListControl.Enabled)
            {
                Template defaultTemplate = TemplateManager.Instance.GetTemplate(0);

                TemplatesComboBox.SelectedIndex = 0;
                TemplateFilterListControl.RestrictedTemplate = defaultTemplate;
                TemplateFilterListControl.Filters = defaultTemplate.Filters;
            }
        }
    }
}
